/*
 * Copyright 2020 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics;

import ohos.agp.text.Font;
import ohos.utils.Pair;

import com.mikepenz.iconics.typeface.IIcon;
import com.mikepenz.iconics.utils.KtExtensions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mikepenz on 30.06.15.
 */
public class IconicsArrayBuilder {
    private final IconicsDrawable iconBase;
    private final List<Pair<Object, Font>> icons = new ArrayList<>();

    /**
     * 构造方法
     *
     * @param iconBase 基础图片
     */
    public IconicsArrayBuilder(IconicsDrawable iconBase) {
        this.iconBase = iconBase;
    }

    /**
     * 添加字体
     *
     * @param icon 字体图标
     * @return 当前builder
     */
    public IconicsArrayBuilder add(IIcon icon) {
        icons.add(new Pair<>(icon, null));
        return this;
    }

    /**
     * 添加字体
     *
     * @param icon 字体图标
     * @param typeface 字体
     * @return 当前builder
     */
    public IconicsArrayBuilder add(String icon, Font typeface) {
        Font tp = typeface;
        if (typeface == null) {
            tp = Font.DEFAULT;
        }
        icons.add(new Pair<>(icon, tp));
        return this;
    }

    /**
     * 添加字体
     *
     * @param icon 字体图标
     * @param typeface 字体
     * @return 当前builder
     */
    public IconicsArrayBuilder add(Character icon, Font typeface) {
        Font result;
        if (typeface == null) {
            result = Font.DEFAULT;
        } else {
            result = typeface;
        }
        icons.add(new Pair<>(icon, result));
        return this;
    }

    /**
     * 构造图片集合
     *
     * @return 图片集合
     */
    public IconicsDrawable[] build() {
        return (IconicsDrawable[]) KtExtensions.map(icons, pair -> {
            IconicsDrawable copy = iconBase.copy();
            final Object ic = pair.f;
            final Font typeface = pair.s;
            copy.setInvalidationEnabled(false);
            if (ic instanceof IIcon) {
                copy.setIcon((IIcon) ic);
            } else if (ic instanceof Character) {
                copy.setIconText(((Character) ic).toString());
                copy.setTypeface(typeface);
            } else if (ic instanceof String) {
                copy.setIconText((String) ic);
                copy.setTypeface(typeface);
            }
            copy.setInvalidationEnabled(true);
            copy.invalidateSelf();
            return copy;
        }).toArray();
    }
}
